package com.qdxht.dispenser.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.qdxht.dispenser.R;
import com.qdxht.dispenser.base.action.TitleBarAction;
import com.qdxht.dispenser.base.action.ToastAction;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.TitleBar;

/**
 * @author GJ
 * @Description: BaseFragment
 * @date 2022/6/2 17:09
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment implements TitleBarAction, ToastAction {

    /** Activity 对象 */
    protected BaseActivity mActivity;

    protected Context context;

    protected T binding;

    /** 标题栏对象 */
    private TitleBar mTitleBar;
    /** 状态栏沉浸 */
    private ImmersionBar mImmersionBar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = getBinding(inflater, container);
        return binding.getRoot();
    }

    protected abstract T getBinding(LayoutInflater inflater, ViewGroup container);

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
        // 获得全局的 Activity
        mActivity = (BaseActivity) requireActivity();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.context = null;
        mActivity = null;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 设置标题栏点击监听
        if (getTitleBar() != null) {
            getTitleBar().setOnTitleBarListener(this);
        }

        if (isStatusBarEnabled()) {
            // 初始化沉浸式状态栏
            getStatusBarConfig().init();

            if (getTitleBar() != null) {
                // 设置标题栏沉浸
                ImmersionBar.setTitleBar(this, getTitleBar());
            }
        }

        initView();
        initData();
    }


    /**
     * 是否在 Fragment 使用沉浸式
     */
    public boolean isStatusBarEnabled() {
        return true;
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    @NonNull
    protected ImmersionBar getStatusBarConfig() {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig();
        }
        return mImmersionBar;
    }

    /**
     * 初始化沉浸式
     */
    @NonNull
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar.with(this)
                // 默认状态栏字体颜色为黑色
                .statusBarDarkFont(isStatusBarDarkFont())
                // 指定导航栏背景颜色
                .navigationBarColor(R.color.white)
                // 状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
                .autoDarkModeEnable(true, 0.2f);
    }

    /**
     * 获取状态栏字体颜色
     */
    protected boolean isStatusBarDarkFont() {
        // 返回真表示黑色字体
        return getAttachActivity().isStatusBarDarkFont();
    }

    @Override
    @Nullable
    public TitleBar getTitleBar() {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar((ViewGroup) getView());
        }
        return mTitleBar;
    }

    /**
     * 获取绑定的 Activity，防止出现 getActivity 为空
     */
    public BaseActivity getAttachActivity() {
        return mActivity;
    }
}
